# @Time : 2020/7/11 10:49
# @Author : Fioman 
# @Phone : 13149920693

"""
在面向对象编程中,协议是非正式接口,只在文档中定义,在代码中不定义.例如Python的序列协议只需要__len__和__getitem__两个方法.任何类(如Spam),
只要使用了标准的签名和语义实现了这两个方法,就能用任何期待序列的地方.Spam是不是哪个类的子类无关紧要,只要提欧共了所需的方法即可.
"""
import collections

Card = collections.namedtuple("Card", ["rank", "suit"])


class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list("JQKA")
    suits = "spades diamonds clubs hearts".split()

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits
                       for rank in self.ranks]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]
